package com.krain.ddbb.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.krain.corelibrary.helper.imageloader.ImageLoader;
import com.krain.corelibrary.widget.loadingView.LoadingView;
import com.krain.corelibrary.widget.recyleview.RefreshRecyclerView;
import com.krain.corelibrary.widget.recyleview.implement.OnRecyclerItemListener;
import com.krain.ddbb.R;
import com.krain.ddbb.adapter.ServiceRemarkAdapter;
import com.krain.ddbb.adapter.UserAddresAdapter;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.PersonArdessBean;
import com.krain.ddbb.entity.ServiceRmarkBean;
import com.krain.ddbb.entity.UserIssueBean;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * Created by ex-kuangxiaomin101 on 2016-02-18.
 */
@EActivity(R.layout.activity_service_remark_list)
public class ServiceRemarkListActivity extends BaseActivity implements RefreshRecyclerView.SwipeRefreshListener {

    @ViewById(R.id.loadView)
    LoadingView mLoadingView;
    @ViewById(R.id.act_remark_list_recyleview)
    RefreshRecyclerView mRefreshRecyclerView;
    int maxPage;
    private ImageLoader mImageLoader;
    private List<ServiceRmarkBean> mEntityList;
    int pageIndex = 1;

    @AfterViews
    void afterView() {
        mRefreshRecyclerView.showRefreshing(true);
        mRefreshRecyclerView.setIsLoadMore(false);
        mRefreshRecyclerView.setSwipeRefreshListener(this);
        mImageLoader = ImageLoader.getInstance(mContext);
        getRemarkList(mEntityList,mRefreshRecyclerView,mLoadingView);
    }

    @Background
    protected void getRemarkList(List<ServiceRmarkBean> mEntityList,RefreshRecyclerView mRefreshRecyclerView,LoadingView loadingView) {
        mEntityList = AppApi.getsInstance(mContext).getRmarkList(app.getmUserinfo().getAccess_token(),pageIndex).getList();
        maxPage = AppApi.getsInstance(mContext).getRmarkList(app.getmUserinfo().getAccess_token(),pageIndex).getPage();
        if(mEntityList!=null){
            if (mEntityList.size() == 0) {
                baseUtil.showSnackBar(this, "暂无数据");
            }
            setAdapter(mEntityList, mRefreshRecyclerView,mLoadingView);
        }
    }

    ServiceRemarkAdapter adapter;
    @UiThread
    protected void setAdapter(List<ServiceRmarkBean> mEntityList,RefreshRecyclerView mRefreshRecyclerView,LoadingView loadingView) {
        mRefreshRecyclerView.setRefreshing(false);
        if (adapter == null){
            if (mEntityList.size()>10)   mRefreshRecyclerView.setIsLoadMore(true);
            adapter = new ServiceRemarkAdapter(mEntityList, mContext,mImageLoader);
            mRefreshRecyclerView.setAdapter(adapter);
            adapter.setOnItemListener(new MyRecyclerItemListener(mEntityList));
            loadingView.showContentView(mRefreshRecyclerView);
        }else {
            if (maxPage == pageIndex) mRefreshRecyclerView.setIsLoadMore(false);
            List<ServiceRmarkBean> list = adapter.getList();
            list.addAll(mEntityList);
            adapter.reflush(list);
            mRefreshRecyclerView.complete();
        }

    }

    @Override
    public void onLoadMore() {
        if (!mRefreshRecyclerView.getCanLoadMore()) return;
        pageIndex ++;
        getRemarkList(mEntityList,mRefreshRecyclerView,mLoadingView);
    }

    @Override
    public void onRefresh() {
        pageIndex = 0;
        adapter = null;
        getRemarkList(mEntityList,mRefreshRecyclerView,mLoadingView);
    }

    /**
     * 列表的点击事件
     */
    class MyRecyclerItemListener extends OnRecyclerItemListener {
        List<ServiceRmarkBean> mEntityList;

        public MyRecyclerItemListener(List<ServiceRmarkBean> mEntityList) {
            this.mEntityList = mEntityList;
        }

        @Override
        public void onItemClick(int position, int viewType, RecyclerView.ViewHolder holder, View v) {

        }
    }

    public static void jumpTothisActivity(BaseActivity activity) {
        Intent intent = new Intent(activity, ServiceRemarkListActivity_.class);
        activity.startActivity(intent);

    }

    @Override
    protected String getToolbarTitle() {
        return "对我的评价";
    }
}
